var app = getApp();
var api = require('../../api.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    flowarray: ['章节测试卷1', '章节测试卷2', '模拟测试2'],
    subject_list: [],
    paper_list: [],
    index: 0,
  },
  flowChange: function (e) {
    this.setData({
      index: e.detail.value
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.mySubjectMistake()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var page = this
    var user = wx.getStorageSync('user_info');
    return {
      path: '/pages/index/index?pid=' + user.user_id,
    }
  },
  //错题记录科目列表
  mySubjectMistake: function () {
    var page = this;
    wx.showLoading({
      title: '加载中',
    })
    app.request({
      url: api.paper.my_subject_mistake,
      method: 'POST',
      data: {
      },
      success: function (res) {
        if (res.errcode == 0) {
          page.setData({
            subject_list: res.data,
          })
          if (res.data.length>0){
            page.myPaperMistake()
          }
          
        }
      },
      fail: function (res) {
        wx.showModal({
          title: '警告',
          content: res.errmsg,
          showCancel: false
        })
      },
      complete: function () {
        wx.hideLoading();
      }
    });
  },
  //错题记录科目下试卷列表
  myPaperMistake: function () {
    var page = this;
    wx.showLoading({
      title: '加载中',
    })
    app.request({
      url: api.paper.my_paper_mistake,
      method: 'POST',
      data: {
        subject_id: page.data.subject_list[page.data.index].subject_id
      },
      success: function (res) {
        if (res.errcode == 0) {
          page.setData({
            paper_list: res.data,
          })
        }
      },
      fail: function (res) {
        wx.showModal({
          title: '警告',
          content: res.errmsg,
          showCancel: false
        })
      },
      complete: function () {
        wx.hideLoading();
      }
    });
  },
  //删除错题
  delMistake:function(e){
    var page = this;
    var paper_id = e.currentTarget.dataset.paper_id;
    wx.showModal({
      title: '提示',
      content: '确定删除错题记录',
      success(res){
        if(res.confirm){
          wx.showLoading({
            title: '加载中',
          })
          app.request({
            url: api.paper.del_mistake,
            method: 'POST',
            data: {
              paper_id: paper_id
            },
            success: function (res) {
              if (res.errcode == 0) {
                wx.showToast({
                  title: '删除成功',
                })
              }
            },
            fail: function (res) {
              wx.showModal({
                title: '警告',
                content: res.errmsg,
                showCancel: false
              })
            },
            complete: function () {
              wx.hideLoading();
            }
          });
        }
      }
    })
  }
})